#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 
# Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
# 

"""
File: 1_encode_cat_features.py
Author: zhangyang(zhangyang40@baidu.com)
Date: 2017/10/20 10:25
"""
import sys
from utils import *
import pandas as pd
import numpy as np

raw_data_path = raw_data_path
tmp_data_path = tmp_data_path

t0org0 = pd.read_csv(raw_data_path + "train.csv", header=0)
h0org = pd.read_csv(raw_data_path + "test.csv", header=0)

if sample_pct < 1.0:
    np.random.seed(999)
    r1 = np.random.uniform(0, 1, t0org0.shape[0])
    t0org0 = t0org0.ix[r1 < sample_pct, :]
    print "testing with small sample of training data, ", t0org0.shape

h0org['click'] = 0
t0org = pd.concat([t0org0, h0org])
print "finished loading raw data, ", t0org.shape

print "to add some basic features ..."
t0org['day'] = np.round(t0org.hour % 10000 / 100)
t0org['hour1'] = np.round(t0org.hour % 100)
t0org['day_hour'] = (t0org.day.values - 21) * 24 + t0org.hour1.values
t0org['day_hour_prev'] = t0org['day_hour'] - 1
t0org['day_hour_next'] = t0org['day_hour'] + 1
t0org['app_or_web'] = 0
t0org.ix[t0org.app_id.values == 'ecad2386', 'app_or_web'] = 1

t0 = t0org

t0['app_site_id'] = np.add(t0.app_id.values, t0.site_id.values)

print "to encode categorical features using mean responses from earlier days -- univariate"
sys.stdout.flush()

calc_exptv(t0, ['app_or_web'])

# exptv_vn_list = ['app_site_id', 'as_domain', 'C14', 'C17', 'C21', 'device_model', 'device_ip', 'device_id', 'dev_ip_aw',
#                  'app_site_model', 'site_model', 'app_model', 'dev_id_ip', 'C14_aw', 'C17_aw', 'C21_aw']
#
# calc_exptv(t0, exptv_vn_list)
# t0.to_csv(raw_data_path + "t0.csv", sep='\t')
#
# calc_exptv(t0, ['app_site_id'], add_count=True)
